﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.IO;

namespace XQ.Framework.html
{
    /// <summary>
    /// xml帮助类
    /// </summary>
    public class XmlHelper
    {
        public static XmlDocument GetXmlDom(string xmlFilePath, string xmlString)
        {
            XmlDocument dom = new XmlDocument();
            if (xmlFilePath != null && File.Exists(xmlFilePath))
            {
                dom.Load(xmlFilePath);
                return dom;
            }
            if (xmlString != null && xmlString.Length > 0)
            {
                dom.LoadXml(xmlString);
                return dom;
            }
            throw new Exception("未能提供有效数据");
        }
        public static XmlNode GetFirstChildNode(XmlNode node)
        {
            return node.FirstChild;
        }
        public static XmlNode GetLastChildNode(XmlNode node)
        {
            return node.LastChild;
        }

        public static XmlNodeList GetChildNodeList(XmlNode node)
        {
            return node.ChildNodes;
        }

        public static XmlNodeList GetNodeList(XmlNode node, string TagName)
        {
            return node.SelectNodes(TagName);
        }

        public static XmlNode GetRootNode(XmlDocument XmlDom, string RootTagName)
        {
            return XmlDom.SelectSingleNode(RootTagName);
        }

        public static string GetNodeAttributeValue(XmlNode node, string AttributeName)
        {
            XmlAttribute attr = node.Attributes[AttributeName];
            return attr == null ? "" : attr.Value;
        }

        public static Dictionary<string, string> GetNodeAttributeDict(XmlNode node)
        {
            Dictionary<string, string> dict = new Dictionary<string, string>();
            XmlAttribute attr = null;
            for (int i = 0; i < node.Attributes.Count; ++i)
            {
                if ((attr = node.Attributes[i]) == null) { continue; }
                dict.Add(attr.Name, attr.Value);
            }
            return dict;
        }

        public static string GetNodeInnerText(XmlNode node)
        {
            return node.InnerText;
        }

        public static XmlNode GetSingleNode()
        {
            return null;
        }
    }
}
